package com.idea.io;

import java.io.StringReader;
import java.io.StringWriter;

import com.hp.hpl.jena.mem.ModelMem;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.vocabulary.RSS;



public class RdfIOKit {

	/*
	public static RDFWriter getPrettyWriter() {
		
		return RdfUtils.getPrettyWriter();
	}
*/
	public static String modelToString(Model model, String lang) {
		//System.out.println("MODEL TO STRPING");
		StringWriter stringOut = new StringWriter();
		try {
			RDFWriter rdfWriter = model.getWriter(lang);
			/*
			rdfWriter.setNsPrefix("features", FEATURES.getURI());
			rdfWriter.setNsPrefix("dc", DCES.getURI());
			rdfWriter.setNsPrefix("rss", RSS.getURI());
			rdfWriter.setNsPrefix("idea", IDEA.getURI());
			rdfWriter.setNsPrefix("graphic", GRAPHIC.getURI());
			rdfWriter.setNsPrefix("fs", FILESYSTEM.getURI());
			rdfWriter.setNsPrefix("prj", PROJECT.getURI());
*/
			// the encoding was screwing up, so declaration removed
			//	rdfWriter.setProperty("showXmlDeclaration", Boolean.FALSE);
			// rdfWriter.write(model, System.out, RSS.getURI());	
			rdfWriter.write(model, stringOut, RSS.getURI());
			//	model.write(stringOut, lang, RSS.getURI());
			// http://base
			stringOut.flush();
			stringOut.close();
		} catch (Exception exception) {
			// exception.printStackTrace();
			// System.exit(1);
			throw new RuntimeException(exception);
		}
		return stringOut.toString();
	}

	//@@TODO this needs pushing out
	public static Model stringToModelExceptionCaught(
		String string,
		String base) {
		Model model = null;

		try {
			model = stringToModel(string, base, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return model;
	}

	public static Model stringToModel(String string, String base, String lang)
		throws Exception {
		Model model = new ModelMem();
		StringReader reader = new StringReader(string);
		model.read(reader, base, lang); // base
		return model;
	}
}
